﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web_cuiki
{
    public class KhuyenMaiDAO
    {
        public static List<KHUYEN_MAI> laydsKieuKhuyenMai()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();

            List<KHUYEN_MAI> ds = null;
            var query = from ksp in db.KHUYEN_MAIs
                        select ksp;
            ds = query.ToList<KHUYEN_MAI>();
            return ds;
        }
        public static KHUYEN_MAI TimKhuyenMaiTheoMa(int maKM)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            var query = from km in db.KHUYEN_MAIs where km.MaKhuyenMai == maKM select km;
            return query.First<KHUYEN_MAI>();
        }
        public static KHUYEN_MAI TimKhuyenMaiTheoPhanTram(int phantram)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            var query = from km in db.KHUYEN_MAIs where km.PhanTram == phantram select km;
            return query.First<KHUYEN_MAI>();
        }
        public static int ThemKhuyenMai(int phantram)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<KHUYEN_MAI> table = db.GetTable<KHUYEN_MAI>();
            try
            {
                KHUYEN_MAI km = new KHUYEN_MAI();
                km.PhanTram = phantram;
                table.InsertOnSubmit(km);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;

        }
        public static int SuaKhuyenMaiTheoMa(int ma, int phantram) {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<KHUYEN_MAI> table = db.GetTable<KHUYEN_MAI>();
            var query = from km in db.KHUYEN_MAIs where km.MaKhuyenMai == ma select km;
            KHUYEN_MAI khuyenmai = query.First<KHUYEN_MAI>();
            try
            {
               
                khuyenmai.PhanTram = phantram;
               // table.InsertOnSubmit(km);
                
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static int XoaKhuyenMaiTheoMa(int maKM) {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            try
            {
                var query = from hd in db.KHUYEN_MAIs where hd.MaKhuyenMai == maKM select hd;
                KHUYEN_MAI hoadon = query.First<KHUYEN_MAI>();
                System.Data.Linq.Table<KHUYEN_MAI> table = db.GetTable<KHUYEN_MAI>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }

    }
}